package cn.bj.xd.mdy.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {
    private static final ObjectMapper jacksonMapper = new ObjectMapper();

    // Jackson动态解析
    public static <T> T toBean(String json, TypeReference<T> typeRef) {
        try {
            return jacksonMapper.readValue(json, typeRef);
        } catch (Exception e) {
            throw new RuntimeException("反序列化失败", e);
        }
    }

    public static <T> T toBean(String json, Class<T> c) {
        try {
            return jacksonMapper.readValue(json, c);
        } catch (Exception e) {
            throw new RuntimeException("反序列化失败", e);
        }
    }


    public static <T> T toBean(String json, JavaType c) {
        try {
            return jacksonMapper.readValue(json, c);
        } catch (Exception e) {
            throw new RuntimeException("反序列化失败", e);
        }
    }
}
